﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "WallTransformationComponent.h"
#include "DbObjectUtils.h"
#include "IGenericElement.h"
#include "Wall.h"
#include "WallPositionPoints.h"
#include "Vector3dUtils.h"
#include "IElement.h"
#include "IElementStatus.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace Sample;


DBOBJECT_DATA_DEFINE(WallTransformationComponent)
{
    SetOwnerElement(nullptr);
}

bool WallTransformationComponent::Transform(const Matrix4d& matrix)
{
    IGenericElement* pGenericElement = quick_cast<IGenericElement>(GetOwnerElement());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pGenericElement, L"所属Element不能为空", L"GDMP", L"2023-09-30");
    Wall* pWall = quick_cast<Wall>(pGenericElement->GetExternalObject());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pWall, L"pWall不能为空", L"GDMP", L"2023-09-30");
    WallData* pData = pWall->GetData();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pData, L"pData不能为空", L"GDMP", L"2023-11-08");
    Vector3d startPt = pData->GetStartPoint();
    Vector3d endPt = pData->GetEndPoint();
    pData->SetStart(Vector3dUtils::TransformPoint(startPt, matrix));
    pData->SetEnd(Vector3dUtils::TransformPoint(endPt, matrix));
    pWall->MarkDataRdId();
    return true;
}

bool WallTransformationComponent::Translate(const gcmp::IElementMoveContext& moveContext)
{
    Vector3d move = moveContext.GetMoveVector();
    Matrix4d moveMatrix;
    moveMatrix.MakeTranslate(move);
    return Transform(moveMatrix);
}

bool WallTransformationComponent::Rotate(const gcmp::Vector3d& origin, const gcmp::Vector3d& axis, double angle)
{
    Matrix4d rotMat;
    rotMat.MakeRotate(origin, axis, angle);
    return Transform(rotMat);
}

bool WallTransformationComponent::CanBeMoved(std::wstring* explanation /*= nullptr*/) const
{
    const IElement* pElement = GetOwnerElement();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElement, L"所属Element不能为空", L"GDMP", L"2023-09-30");
    return pElement->GetStatus()->CanBeMoved();
}

bool WallTransformationComponent::CanBeRotated(std::wstring* explanation /*= nullptr*/) const
{
    const IElement* pElement = GetOwnerElement();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElement, L"所属Element不能为空", L"GDMP", L"2023-09-30");
    return pElement->GetStatus()->CanBeRotated();
}

bool WallTransformationComponent::Scale(const gcmp::Vector3d& basePt, float scaleFactor)
{
    gcmp::Matrix4d scaleMat = Matrix4d::CreateIdentityMatrix4d();
    scaleMat.MakeScale(basePt, scaleFactor);
    return Transform(scaleMat);
}

gcmp::NdbObject* WallTransformationComponent::GetTopOwnerObject() const
{
    return quick_cast<gcmp::NdbObject>(m_pOwnerElement);
}

